# coding: utf-8
# Create your views here.
from django.views.generic.simple import direct_to_template
from recette.models import Recette
from recette.forms import RechercheRecette

def index(request):
    
    if request.method == "POST" :
        form = RechercheRecette(data=request.POST)
        if form.is_valid():
            results = Recette.objects.filter(type = request.POST['type'])
            for id in [int(id) for id in request.POST.getlist('ingredients')]:
                results = results.filter(ingredients = id)
            results = results.order_by('titre')
            return direct_to_template(request,'index.html',{'form':form,
                                                            'results':results})
        else :
            return direct_to_template(request,'index.html',{'form':form})
    else :
        form = RechercheRecette()
        return direct_to_template(request,'index.html',{'form':form})

def fiche(request, recette_id=None):
    
    recette = Recette.objects.get(id=recette_id)
    return direct_to_template(request,'fiche.html',{'recette':recette})